﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Lyon.Util.Tools;

namespace Lyon.Util.Helper
{
    public static class StringHelper
    {
        /// <summary>
        /// 判断字符串是否为空或者null
        /// </summary>
        /// <param name="s"></param>
        /// <returns>true-空/null false-非空</returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        /// <summary>
        /// 用args格式化字符串s
        /// </summary>
        /// <param name="s"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string FormatWith(this string s, params object[] args)
        {
            return string.Format(s, args);
        }

        /// <summary>
        /// 判断字符串s是否满足正则表达式pattern
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsMatch(this string s, string pattern)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, pattern);
        }

        /// <summary>
        /// 返回字符串s匹配正则表达式pattern的结果
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string Match(this string s, string pattern)
        {
            if (s == null) return string.Empty;
            else return Regex.Match(s, pattern).Value;
        }

        /// <summary>
        /// 判断字符串是否为整型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInt(this string s)
        {
            return TypeParser.IsInteger(s);
        }

        /// <summary>
        /// 判断字符串是否为数字
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNumber(this string s)
        {
            return TypeParser.IsNumber(s);
        }

        /// <summary>
        /// 将字符串转换为整型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int ToInt(this string s)
        {
            return TypeParser.String2Int(s);
        }

        /// <summary>
        ///  将字符串转换为浮点型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static float ToFloat(this string s)
        {
            return TypeParser.String2Float(s);
        }

        /// <summary>
        ///  将字符串转换为双精度型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static double ToDouble(this string s)
        {
            return TypeParser.String2Double(s);
        }

        /// <summary>
        /// 将字符串转化为Camel规范。例如：helloWorld
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToCamel(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToLower() + s.Substring(1);
        }

        /// <summary>
        /// 将字符串转化为Pascal规范。例如：HelloWorld
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToPascal(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToUpper() + s.Substring(1);
        }

        /// <summary>
        /// 将字符串转换为日期
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string s)
        {
            return TypeParser.String2DateTime1(s);
        }

        /// <summary>
        /// 判断字符串是否为Email
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsEmail(this string s)
        {
            return s.IsMatch(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        }

        /// <summary>
        /// 获得字符串的字节数组
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] GetBytes(this string s)
        {
            if (s.IsNullOrEmpty()) return new byte[0];
            return System.Text.Encoding.Default.GetBytes(s);
        }

        /// <summary>
        /// 将字符串按照制定编码进行解码
        /// </summary>
        /// <param name="s"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Decode(this string s, Encoding encoding)
        {
            if (s.IsNullOrEmpty()) return string.Empty;
            return s.GetBytes().Decode(encoding);
        }

        /// <summary>
        /// 字符串转换为全角(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        public static string ToSBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

        /// <summary>
        /// 字符串转换为半角(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToDBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }
    }
}
